﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Clotho.Novelview {
	public partial class Resizer : UserControl {
		public Resizer() {
			InitializeComponent();
		}
		public readonly List<Control> ResizeControls = new List<Control>();

		public Action<Object> dResizeDone;

		private void resize_MouseDown(object sender, MouseEventArgs e) {
			Point mouse_begin;
			Point moust_last;
			Button b = sender as Button;
			if (e.Button == MouseButtons.Left) {
				moust_last = mouse_begin = MousePosition;
				b.MouseMove += Br_MouseMove;
			}
			void Br_MouseMove(object sender2, MouseEventArgs e2) {
				if (MouseButtons == MouseButtons.Left) {
					var mouse_end = MousePosition;
					if (br == b) {
						Size = Size + new Size(mouse_end.X - moust_last.X, mouse_end.Y - moust_last.Y);
						foreach(var c in ResizeControls) {
							c.Size = c.Size + new Size(mouse_end.X - moust_last.X, mouse_end.Y - moust_last.Y);
						}
					} else if (tr == b){
						Size = Size + new Size(mouse_end.X - moust_last.X, moust_last.Y - mouse_end.Y );
						Top  -= (moust_last.Y - mouse_end.Y);
						foreach (var c in ResizeControls) {
							c.Size = c.Size + new Size(mouse_end.X - moust_last.X, moust_last.Y - mouse_end.Y);
							c.Top -= (moust_last.Y - mouse_end.Y);
						}
					} else if (bl == b) {
						Size = Size + new Size(moust_last.X - mouse_end.X, mouse_end.Y - moust_last.Y);
						Left -= (moust_last.X - mouse_end.X);
						foreach (var c in ResizeControls) {
							c.Size = c.Size + new Size(moust_last.X - mouse_end.X, mouse_end.Y - moust_last.Y);
							c.Left -= (moust_last.X - mouse_end.X);
						}

					} else if (tl == b) {
						Size = Size + new Size(moust_last.X - mouse_end.X, moust_last.Y - mouse_end.Y);
						Top -= (moust_last.Y - mouse_end.Y);
						Left -= (moust_last.X - mouse_end.X);
						foreach (var c in ResizeControls) {
							c.Size = c.Size + new Size(moust_last.X - mouse_end.X, moust_last.Y - mouse_end.Y);
							c.Top -= (moust_last.Y - mouse_end.Y);
							c.Left -= (moust_last.X - mouse_end.X);
						}
					}
					moust_last = mouse_end;
				} else {
					b.MouseMove -= Br_MouseMove;
					dResizeDone?.Invoke(this);
				}
			}
		}






	}
}
